<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/10/29
 * Time: 13:20
 */

namespace App\Http\Controllers\Web;


use App\Http\Controllers\ApiController;
use App\Http\Controllers\Controller;
use App\Model\About;
use App\Model\Apply;
use App\Model\Banner;
use App\Model\Cases;
use App\Model\Job;
use App\Model\News;
use App\Model\NewsCategory;
use App\Model\Page;
use App\Model\Partner;
use App\Model\Style;
use App\Model\ServiceProcess;
use App\Model\Team;
use App\Model\TeamCategory;
use App\Model\Upload;
use App\Rules\Mobile;
use App\Services\AlibabaServices;
use Illuminate\Http\Request;

class IndexController extends ApiController
{
    public function index(){
        $banners = Banner::all();

        $styles = Style::all();

        $partners = Partner::all();

        $news = News::query()->limit(6)->get();

        return view($this->view.'index',compact('banners', 'styles', 'partners', 'news'));
    }
    public function about(){
        $about = About::query()->with('image')->get()->first()->toArray();
        $about['image_url'] = \Storage::disk($about['image']['disk'])->url($about['image']['path']);

        $process = ServiceProcess::with('image')->get()->toArray();
        foreach ($process as &$prcs){
            $prcs['image_url'] = \Storage::disk($prcs['image']['disk'])->url($prcs['image']['path']);
            $prcs['icon_url'] = \Storage::url($prcs['icon']);
        }

        return view($this->view.'about',compact('about', 'process'));
    }
    public function case(Request $request){

        $where = [];
        $query = $request->only(['style_id','item_cate','status','order']);
        if(isset($query['style_id'])){
            $where['style_id'] = $query['style_id'];
        }
        if(isset($query['item_cate'])){
            $where['item_cate'] = $query['item_cate'];
        }
        if(isset($query['status'])){
            $where['status'] = $query['status'];
        }
        $order = 'sort';
        if(isset($query['order'])){
            switch ($query['order']){
                case 'flag':
                    $order = 'flag';
                    break;
                case 'hot':
                    $order = 'hot';
                    break;
            }
        }

        $styles = Style::query()->get();

        $cases = Cases::query()->with('image')->where($where)->orderBy($order,'desc')->paginate();
        $status = Cases::$status;
        foreach ($cases as &$cv){
            $cv['image_url'] = \Storage::disk($cv['image']['disk'])->url($cv['image']['path']);
            $cv['images'] = Upload::query()->whereIn('id',$cv['images'])->get()->map(function($value){
                return \Storage::disk($value['disk'])->url($value['path']);
            })->toArray();
        }

        return view('case', compact('cases','query', 'status','styles'));
    }
    public function caseDetails($id){
        $info = Cases::query()->find($id);
        $info['images'] = Upload::query()->whereIn('id',$info['images'])->get()->map(function($value){
            return \Storage::disk($value['disk'])->url($value['path']);
        })->toArray();
        $skey = 'case_detail_'.$id;
        if(!session($skey)){
            $info->increment('click');
            session([$skey=>1]);
        }
        return view('case_details',compact('info'));
    }
    public function team(){
        $categorys = TeamCategory::query()->where(['pid'=>0])->get();
        return view('team',compact('teams','categorys'));
    }
    public function teamDetails($id){
        $info = Team::query()->find($id);
        return view('team_details',compact('info'));
    }
    public function news(Request $request){

        $query = $request->only(['category_id']);
        $where = [];
        if(isset($query['category_id'])){
            $where['category_id'] = $query['category_id'];
        }
        $news = News::query()->where($where)->orderBy('id','desc')->paginate();
        $categorys = NewsCategory::all();

        return view('news', compact('news','query','categorys'));
    }
    public function newsDetails($id){
        $info = News::query()->find($id);

        $skey = 'news_detail_'.$id;
        if(!session($skey)){
            $info->increment('click');
            session([$skey=>1]);
        }

        return view('news_details',compact('info'));
    }
    public function site(){
        return view('site');
    }
    public function service(){
        $afterSaleInfo = Page::query()->find(1);
        return view('service',compact('afterSaleInfo'));
    }
    public function apply(Request $request){
        $data = $request->only(['address','measure_area','name','phone']);
        $this->validate($request,[
            'address' => 'required',
            'measure_area' => 'required',
            'name' => 'required',
            'phone' => ['required',new Mobile()],
        ]);
        $model = new Apply();
        $model->fill($data);
        if($model->save()){
            $ali = new AlibabaServices();
            $result = $ali->SendSms('SMS_177542085',15268888209);

            return $this->message('预约成功！');
        }else{
            return $this->failed('预约失败！');
        }

    }
    public function contact(){
        $jobs = Job::all();
        return view('contact',compact('jobs'));
    }
}
